package com.hgyc.mom.wms.controller;

import java.util.*;

import com.hgyc.mom.wms.vo.ScmPurchaseOrderVO;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.wms.entity.ScmPurchaseOrder;
import com.hgyc.mom.wms.vo.ScmPurchaseOrderParam;
import com.hgyc.mom.wms.service.ScmPurchaseOrderService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 采购订单Controller
 *
 * @author fwj
 * @since 2025-05-15
 */
@Tag(name = "采购订单控制器", description = "采购订单控制器")
@RestController
@RequestMapping("/wms/po")
public class ScmPurchaseOrderController {
    @Autowired
    private ScmPurchaseOrderService scmPurchaseOrderService;

    @Operation(summary = "查询采购订单列表", description = "查询采购订单列表")
    @PostMapping("/pageList")
    public Result<IPage<ScmPurchaseOrder>> pageList(@Parameter(description = "查询参数") @RequestBody ScmPurchaseOrderParam scmPurchaseOrderParam) {
        Page<ScmPurchaseOrder> page = new Page<>(scmPurchaseOrderParam.getPage(), scmPurchaseOrderParam.getPageSize());
        ScmPurchaseOrder scmPurchaseOrder = new ScmPurchaseOrder();
        BeanUtils.copyProperties(scmPurchaseOrderParam, scmPurchaseOrder);
        QueryWrapper<ScmPurchaseOrder> wrapper = QueryWrapperUtil.build(scmPurchaseOrder);
        IPage<ScmPurchaseOrder> list = scmPurchaseOrderService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "更新状态", description = "更新状态")
    @PutMapping("/status/{status}")
    public Result<?> updateStatus(@PathVariable("status") String status, @RequestParam("ids") Long[] ids) {
        scmPurchaseOrderService.updateStatus(status, Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ScmPurchaseOrder> util = new ExcelUtil<>(ScmPurchaseOrder.class);
        util.importTemplateExcel(response, "采购订单");
    }

    @Operation(summary = "导出采购订单列表", description = "导出采购订单列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmPurchaseOrderParam scmPurchaseOrderParam) {
        ScmPurchaseOrder scmPurchaseOrder = new ScmPurchaseOrder();
        BeanUtils.copyProperties(scmPurchaseOrderParam, scmPurchaseOrder);
        QueryWrapper<ScmPurchaseOrder> wrapper = QueryWrapperUtil.build(scmPurchaseOrder);
        List<ScmPurchaseOrder> list = scmPurchaseOrderService.list(wrapper);
        ExcelUtil<ScmPurchaseOrder> util = new ExcelUtil<>(ScmPurchaseOrder.class);
        util.exportExcel(response, list, "采购订单数据");
    }

    @Operation(summary = "导入采购订单列表", description = "导入采购订单列表")
    @PostMapping("/import")
    public Result importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ScmPurchaseOrder> util = new ExcelUtil<>(ScmPurchaseOrder.class);
        List<ScmPurchaseOrder> scmPurchaseOrderList = util.importExcel(file.getInputStream());
        String message = scmPurchaseOrderService.importData(scmPurchaseOrderList, updateSupport);
        return Result.success(message);
    }

    @Operation(summary = "获取采购订单详细信息", description = "获取采购订单详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "采购订单ID") @PathVariable("id") Long id) {
        return Result.success(scmPurchaseOrderService.getById(id));
    }

    @Operation(summary = "新增采购订单", description = "新增采购订单")
    @PostMapping
    public Result save(@Parameter(description = "采购订单") @RequestBody ScmPurchaseOrderVO scmPurchaseOrderVO) {
        scmPurchaseOrderService.savePurChaseOrder(scmPurchaseOrderVO);
        return Result.success();
    }

    @Operation(summary = "修改采购订单", description = "修改采购订单")
    @PutMapping
    public Result update(@Parameter(description = "采购订单") @RequestBody ScmPurchaseOrderVO scmPurchaseOrderVO) {
        scmPurchaseOrderService.updatePurChaseOrder(scmPurchaseOrderVO);
        return Result.success();
    }

    @Operation(summary = "删除采购订单", description = "删除采购订单")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        scmPurchaseOrderService.removePurChaseOrder(Arrays.asList(ids));
        return Result.success();
    }
}
